package htmodels

import (
	"github.com/astaxie/beego/orm"
	"shuxianghui/models"
)

func HTUsersAdd(users *models.Users) error {
	o := orm.NewOrm()
	_, err := o.Insert(users)
	return err
}

func HTUsersGetDetail(users *models.Users) error {
	o := orm.NewOrm()
	return o.Read(users)
}

func HTUsersGetCount(search string, searchFiled string, UserType int) (int64, error) {
	o := orm.NewOrm()
	m := models.Users{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_UserType(), UserType).Filter(searchFiled + "__icontains", search).Count()
	return count, err
}

func HTUsersGetList(userss *[]models.Users, fields []string, UserType int, offset int64, limit int, search string, searchFiled string, orderby string) error {
	o := orm.NewOrm()
	m := models.Users{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_UserType(), UserType).Filter(searchFiled + "__icontains", search).OrderBy(orderby).Offset(offset).Limit(limit).All(userss, fields...)
	return err
}

func HTUsersDelete(Ids []int64) (int64, error) {
	o := orm.NewOrm()
	m := models.Users{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Delete()
	return count, err
}

func HTUsersUpdate(users *models.Users) error {
	o := orm.NewOrm()
	_, err := o.Update(users)
	return err
}

//return true if pass and name(phone) right
func HTCheckUserLogin(m *models.Users, user string, pass string, emailOrPhone bool) bool {
	o := orm.NewOrm()
	querybuilder := o.QueryTable(m.TableName())
	if emailOrPhone {
		querybuilder = querybuilder.Filter(m.FN_Email(), user)
	} else {
		querybuilder = querybuilder.Filter(m.FN_Phone(), user)
	}
	if err := querybuilder.One(m); err != nil {
		return false
	}
	if pass != m.Password {
		return false
	}
	return true
}

func HTUserGuanliDelete(m *models.Users) {
	o := orm.NewOrm()
	o.QueryTable(m.TableName()).Filter(m.FN_CityId(), m.CityId).Filter(m.FN_UserType(), m.UserType).Delete()
}

func HTUserGetUserByPhone(user *models.Users) error {
	o := orm.NewOrm()
	return o.QueryTable(user.TableName()).Filter(user.FN_Phone(), user.Phone).One(user)
}

func HTGetUsersByCityAndType(users *[]models.Users, cityid int, usertype int) error {
	o := orm.NewOrm()
	m := models.Users{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_CityId(), cityid).Filter(m.FN_UserType(), usertype).All(users)
	return err
}

func GetLocalUserCount(cityid int, usertype int, extends ...interface{}) (int64, error) {
	o := orm.NewOrm()
	m := models.Users{}
	flagCity := true
	qs := o.QueryTable(m.TableName())
	for _, arg := range extends {
		//迭代不定参数
		switch arg.(type) {
		case []CityId_int:
			if len(arg.([]CityId_int)) > 0 {
				flagCity = false
				qs = qs.Filter(m.FN_CityId() + "__in", arg.([]CityId_int))
			}
		}
	}
	if cityid != 0 {
		qs = qs.Filter(m.FN_CityId(), cityid)
	}
	if usertype != -1 && flagCity {
		qs = qs.Filter(m.FN_UserType(), usertype)
	}
	return qs.Count()
}

func UpdatePassword(pass string, phone string) error {
	o := orm.NewOrm()
	m := models.Users{Phone:phone}
	if err := o.Read(&m, m.FN_Phone()); err != nil {
		return err
	}
	m.Password = pass
	if _, err := o.Update(&m, m.FN_Password()); err != nil {
		return err
	}
	return nil
}

func GetUserIdByPhone(phone string) int {
	o := orm.NewOrm()
	m := models.Users{Phone:phone}
	if err := o.Read(&m, m.FN_Phone()); err != nil {
		return 0
	}
	return m.Id
}

func AddMark(user *models.Users, mark int, add bool) (err error) {
	o := orm.NewOrm()

	if add {
		_, err = o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{
			user.FN_MarksOfAdmin(): orm.ColValue(orm.ColAdd, mark),
			user.FN_Mark():orm.ColValue(orm.ColAdd, mark),
		})
	} else {
		_, err = o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{
			user.FN_MarksOfAdmin(): orm.ColValue(orm.ColMinus, mark),
			user.FN_Mark():orm.ColValue(orm.ColMinus, mark),
		})
	}
	return err
}

func AddMarkOfCaculate(user *models.Users, mark int) (err error) {
	o := orm.NewOrm()

	_, err = o.QueryTable(user.TableName()).Filter(user.FN_Id(), user.Id).Update(orm.Params{
		user.FN_MarkOfCaculate(): orm.ColValue(orm.ColAdd, mark),
		user.FN_Mark():orm.ColValue(orm.ColAdd, mark),
	})

	return err
}

const PublishMark int = 20
const RegitstMark int = 50

const (
	LogType_MarkOfAdmin int = 1
	LogType_MarkOfCaculate
	LogType_ArticlePublish
	LogType_ArticleDeny
	LogType_ArticleShenghe
	LogType_ArticleStatusChange
	LogType_Login
	LogType_ArticleUpdate
	LogType_ArticleAdd
	LogType_ArticleDelete
	LogType_MarkInit
)

func InsertLog(m *models.SQLLogs) error {
	o := orm.NewOrm()
	_, err := o.Insert(m)
	return err
}